﻿using BotDetector.Core.Model;
using BotDetector.Core.Detector;
using BotDetector.Core.Search;
using System.Diagnostics;
using System;

namespace BotDetector.ConsoleApplication
{
    public class ResultAnalyzer
    {

        private const string googleSearchStarted = "Google search started";
        private const string googleSearchFinished = "Google search finished";
        private const string bingSearchStarted = "Bing search started";
        private const string bingSearchFinished = "Bing search finished";
        private const string typeInPhrase = "Please type in a phrase :";
        private const string analyzeStarted = "Analyze started";
        private const string analyzeFinished = "Analyze finished";
      
        private ResultItemList results;
        private ISearch searchEngine;
        private IBotDetector detector = new BotDetector.Core.Detector.BotDetector();

        public string phrase { get; set; }
        public bool isBot { get; private set; }

        public void GoogleExecute()
        {
            searchEngine = new GoogleSearch();
            Trace.WriteLine(googleSearchStarted);
            results = searchEngine.Search(phrase);
            Trace.WriteLine(googleSearchFinished);
            Trace.WriteLine(analyzeStarted);         
            isBot = detector.IsBot(results);
            Trace.WriteLine(analyzeFinished);
        }

        public void BingExecute()
        {
            searchEngine = new BingSearch();
            Trace.WriteLine(bingSearchStarted);
            results = searchEngine.Search(phrase);
            Trace.WriteLine(bingSearchFinished);
            Trace.WriteLine(analyzeStarted);           
            isBot = detector.IsBot(results);
            Trace.WriteLine(analyzeFinished);
        }

        public void GoogleBingExecute()
        {
            searchEngine = new GoogleSearch();
            Trace.WriteLine(googleSearchStarted);
            results = searchEngine.Search(phrase);
            Trace.WriteLine(googleSearchFinished);
            searchEngine = new BingSearch();
            Trace.WriteLine(bingSearchStarted);
            ResultItemList results2 = searchEngine.Search(phrase);
            Trace.WriteLine(bingSearchFinished);
            GoogleBingAggregator aggregator = new GoogleBingAggregator();
            results = aggregator.AggregateGoogleAndBingResultsItemLists(results, results2);
            Trace.WriteLine(analyzeStarted);
            isBot = detector.IsBot(results);
            Trace.WriteLine(analyzeFinished);
        }

    }
}
